package com.yicoollxing.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yicoollxing.common.Const;
import com.yicoollxing.common.ServerResponse;
import com.yicoollxing.dao.mapper.UserMapper;
import com.yicoollxing.pojo.User;
import com.yicoollxing.service.IUserService;
import com.yicoollxing.util.MD5Util;


@Service("iUserService")
public class UserServiceImpl implements IUserService {
	
	
	
	//banckend
	@Autowired
	private UserMapper userMapper;
	
	public ServerResponse  doLogin(String username,String password){
		
		int resultraw=userMapper.selectByUsername(username);
		if(resultraw==0){
	     	return	ServerResponse.createByErrorMessage("用户名已存在");
		}
		String md5Password=MD5Util.MD5EncodeUtf8(password);
		User user=userMapper.selectByUsernameAndPassword(username, md5Password);
		if(user==null){
			ServerResponse.createByErrorMessage("用户名或密码不正确");
		}
		
		return ServerResponse.createBySuccess("登录成功", user);
	}
	
	

	public ServerResponse<String>  checkAdminRole(User user){
			if(user!=null&&user.getRole()==Const.Role.ROLE_ADMIN){
				return ServerResponse.createBySuccess();
			}
			return ServerResponse.createByErrorMessage("无权限操作，需要管理员权限");
		}

}
